package com.yiwenup.struct._01_linear.list;

import com.yiwenup.struct._01_linear.base.AbstractList;

/**
 * 双向链表
 */
public class DoublyLinkedList<E> extends AbstractList<E> {
    private Node<E> head;
    private Node<E> tail;

    private static class Node<E> {
        private E element;
        private Node<E> prev;
        private Node<E> next;

        public Node(E element, Node<E> prev, Node<E> next) {
            this.element = element;
            this.prev = prev;
            this.next = next;
        }
    }

    @Override
    public void clear() {
        size = 0;
        head = null;
        tail = null;
    }

    @Override
    public E get(int idx) {
        return node(idx).element;
    }

    @Override
    public E set(int idx, E ele) {
        Node<E> node = node(idx);
        E old = node.element;
        node.element = ele;
        return old;
    }

    @Override
    public void insert(int idx, E ele) {
        rangeCheckForAdd(idx);

        if (idx == size) {
            Node<E> oldTail = tail;
            tail = new Node<>(ele, oldTail, null);
            if (oldTail == null) {
                head = tail;
            } else {
                oldTail.next = tail;
            }
        } else {
            Node<E> next = node(idx);
            Node<E> prev = next.prev;
            Node<E> node = new Node<>(ele, prev, next);
            next.prev = node;
            if (prev == null) {
                head = node;
            } else {
                prev.next = node;
            }
        }

        size++;
    }

    @Override
    public E remove(int idx) {
        rangeCheck(idx);
        Node<E> node = node(idx);
        Node<E> prev = node.prev;
        Node<E> next = node.next;
        if (prev == null ) {
            head = next;
        } else {
            prev.next = node.next;
        }
        if (next == null) {
            tail = prev;
        } else {
            next.prev = node.prev;
        }
        size--;
        return node.element;
    }

    @Override
    public E remove(E ele) {
        return remove(indexOf(ele));
    }

    @Override
    public int indexOf(E ele) {
        if (ele == null) {
            Node<E> node = head;
            for (int i = 0; i < size; i++) {
                if (node.element == null) {
                    return i;
                }
                node = node.next;
            }
        } else {
            Node<E> node = head;
            for (int i = 0; i < size; i++) {
                if (ele.equals(node.element)) {
                    return i;
                }
                node = node.next;
            }
        }
        return -1;
    }

    /**
     * 获取idx位置的node对象
     *
     * @param idx 索引位置
     * @return node对象
     */
    private Node<E> node(int idx) {
        rangeCheck(idx);

        Node<E> node;
        if (idx < (size >> 1)) {
            node = this.head;
            for (int i = 0; i < idx; i++) {
                node = node.next;
            }
        } else {
            node = this.tail;
            for (int i = size - 1; i > idx; i--) {
                node = node.prev;
            }
        }
        return node;
    }
}
